package com.gg.registration.hospital.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gg.common.result.CommonResult;
import com.gg.common.result.ResultCodeEnum;
import com.gg.common.utils.RegistrationException;
import com.gg.registration.hospital.mapper.HospitalSetMapper;
import com.gg.registration.hospital.service.HospitalSetService;
import com.gg.registration.model.hosp.HospitalSet;
import com.gg.registration.vo.hosp.HospitalSetQueryVo;
import com.gg.registration.vo.order.SignInfoVo;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;

/**
 * ServiceImpl: IService 实现类（ 泛型：M 是 mapper 对象，T 是实体 ）
 */
@Service
public class HospitalSetServiceImpl extends ServiceImpl<HospitalSetMapper, HospitalSet> implements HospitalSetService {

    /**
     *  @Autowired
     *  protected M baseMapper;
     *  已经自动实现自动注入dao，太牛了
     */


    /**
     * 分页查询
     * @param vo 前端传参实体类
     * @return 包装对象
     */
    public QueryWrapper pageQuery(HospitalSetQueryVo vo) {
        QueryWrapper wrapper = new QueryWrapper();
        if (null != vo){
            if (null != vo.getHosname()) {
                wrapper.like("hosname", vo.getHosname());
            }
            if (null != vo.getHoscode() && vo.getHoscode() != "") {
                // 使用like 会导致索引失效
                // wrapper.like("hoscode", vo.getHoscode());
                wrapper.eq("hoscode", vo.getHoscode());
            }
        }
        return wrapper;
    }

    @Override
    public String getSignKey(String hoscode) {
        QueryWrapper<HospitalSet> wrapper = new QueryWrapper<>();
        wrapper.eq("hoscode",hoscode);
        HospitalSet hospitalSet = baseMapper.selectOne(wrapper);
        return hospitalSet.getSignKey();
    }

    /**
     * 订单调用：获取医院签名信息
     * @param hoscode 医院编号
     * @return 签名信息实体类，包含签名信息和url
     */
    @Override
    public SignInfoVo getSignInfoVo(String hoscode) {
        QueryWrapper<HospitalSet> wrapper = new QueryWrapper<>();
        wrapper.eq("hoscode",hoscode);
        HospitalSet hospitalSet = baseMapper.selectOne(wrapper);
        if(null == hospitalSet) {
            throw new RegistrationException(ResultCodeEnum.HOSPITAL_OPEN);
        }
        SignInfoVo signInfoVo = new SignInfoVo();
        signInfoVo.setApiUrl(hospitalSet.getApiUrl());
        signInfoVo.setSignKey(hospitalSet.getSignKey());
        return signInfoVo;
    }
}
